package com.forlove.app.qselect.friend.presenter;

import android.text.TextUtils;

import com.forlove.app.ForloveApplication;
import com.forlove.app.common.base.mvp.presenter.IBasePresenter;
import com.forlove.app.common.network.ICallBack;
import com.forlove.app.common.utils.LogUtil;
import com.forlove.app.common.utils.ParseHelper;
import com.forlove.app.manager.FriendManager;
import com.forlove.app.manager.NetworkManager;
import com.forlove.app.manager.ServerManager;
import com.forlove.app.qselect.CommonEntity;
import com.forlove.app.qselect.friend.entity.MineFriendEntity;
import com.forlove.app.qselect.friend.service.INewFriendService;
import com.forlove.app.qselect.friend.service.impl.NewFriendServiceImpl;
import com.forlove.app.qselect.friend.view.NewFriendView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by yjl on 2017/10/25.
 */

public class NewFriendPresenter extends IBasePresenter {
    private INewFriendService newFriendService;
    private NewFriendView newFriendView;

    public NewFriendPresenter(NewFriendView newFriendView) {
        this.newFriendView = newFriendView;
        if (newFriendService == null) {
            newFriendService = new NewFriendServiceImpl();
            setService(newFriendService);
        }
    }

    public void loadNewFriendData(Map param) {
        newFriendService.loadData(ServerManager.ACTION_NEW_FRIEND, param, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                List<MineFriendEntity> mineFriendEntities = new ArrayList<>();
                mineFriendEntities = ParseHelper.getModelList(result, "data.friends", MineFriendEntity.class);
                FriendManager.saveNewFriends(result);
                if (mineFriendEntities != null && mineFriendEntities.size() > 0) {
                    MineFriendEntity friendEntity = mineFriendEntities.get(0);
                    long addtime = friendEntity.addtime;
                    FriendManager.saveNewFriendAddTime(ForloveApplication.getInstance(), addtime);
                }
                if (newFriendView != null) {
                    newFriendView.showNewFriendData(mineFriendEntities);
                }
            }

            @Override
            public void onFail(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    if (newFriendView != null) {
                        newFriendView.showErrorData(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                    }
                } else {
                    if (newFriendView != null) {
                        newFriendView.showErrorData(NetworkManager.ResultCode.RESULT_ERROR, "稍后重试!");
                    }
                }
            }
        });
    }

    public void loadAgreeFriendData(Map param) {
        newFriendService.loadData(ServerManager.ACTION_AGREE_FRIEND, param, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                LogUtil.e("yjl", "同意好友请求!");
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (commonEntity.state == NetworkManager.ResultCode.RESULT_SUCCESS.getValue()) {
                    if (newFriendView != null) {
                        newFriendView.showAgreeFriend(NetworkManager.ResultCode.RESULT_SUCCESS, "添加成功!");
                    }
                }
            }

            @Override
            public void onFail(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    if (newFriendView != null) {
                        newFriendView.showErrorData(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                    }
                } else {
                    if (newFriendView != null) {
                        newFriendView.showErrorData(NetworkManager.ResultCode.RESULT_ERROR, "稍后重试!");
                    }
                }
            }
        });
    }


    public void loadDeleteFriendRecord(Map param) {
        newFriendService.loadData(ServerManager.ACTION_DELETE_FRIEND_RECORD, param, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                newFriendView.showDeleteRecordView(commonEntity);
            }

            @Override
            public void onFail(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    if (newFriendView != null) {
                        newFriendView.showErrorData(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                    }
                } else {
                    if (newFriendView != null) {
                        newFriendView.showErrorData(NetworkManager.ResultCode.RESULT_ERROR, "稍后重试!");
                    }
                }
            }
        });
    }
}
